package com.yydream.common.config;

import com.yydream.common.constant.HttpStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig () {

        List<ResponseMessage> responseList = new ArrayList<>();

        Arrays.stream(HttpStatus.values()).forEach(resultCode -> {
            ResponseMessageBuilder responseMessageBuilder = new ResponseMessageBuilder();
            responseMessageBuilder.code(resultCode.getCode());
            responseMessageBuilder.message(resultCode.getMsg());
            ResponseMessage build = responseMessageBuilder.build();
            responseList.add(build);
        });



        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.groupName("base")
        .apiInfo(getInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.yydream.controller"))
        .build();

        docket.ignoredParameterTypes(ServletResponse.class)
                .useDefaultResponseMessages(false)
                .globalResponseMessage(GET, responseList)
                .globalResponseMessage(POST, responseList)
                .globalResponseMessage(PUT, responseList)
                .globalResponseMessage(DELETE, responseList);
        return docket;
    }

    public ApiInfo getInfo () {
        return new ApiInfoBuilder()
                .title("仓库管理接口文档")
                .description("仓库管理接口文档")
                .version("1.0.1")
                .contact(new Contact("yydream", "yydream.top", "411***36@qq.com"))
                .build();
    }
}
